#include "EntityManager.h"

#include <SFML/Graphics.hpp>

#include "GameService.h"
#include "WindowManager.h"
#include "LevelManager.h"
#include "../Data/Node.h"

using namespace std;

#include "../Entities/GameEntity.h"
#include "../Entities/PlayerEntity.h"
#include "../Entities/SimpleAIEntity.h"

//-------------------------------------------------------------------------------------------
EntityManager::EntityManager()
{
}

//-------------------------------------------------------------------------------------------
EntityManager::~EntityManager()
{
}

//-------------------------------------------------------------------------------------------
void EntityManager::Setup()
{
	m_pWindowManager = GameService::GetInstance()->GetWindowManager();
	m_pLevelManager = GameService::GetInstance()->GetLevelManager();
}

//-------------------------------------------------------------------------------------------
void EntityManager::Tick(float DeltaTime)
{
	for (int i = 0; i < m_entityMap.size(); i++)
	{
		// Get the current entity
		GameEntity* pEntity = m_entityMap[i];

		//Render it
		Node* pFromNode = m_pLevelManager->GetNode(pEntity->GetCurrentNodeID());
		Node* pToNode = m_pLevelManager->GetNode(pEntity->GetDestinationNodeID());

		if (!pToNode || !pFromNode)
			continue;

		float posX = pFromNode->GetX() + ((pToNode->GetX() - pFromNode->GetX()) * pEntity->GetProgress());
		float posY = pFromNode->GetY() + ((pToNode->GetY() - pFromNode->GetY()) * pEntity->GetProgress());
		

		sf::RenderWindow* pWindow = m_pWindowManager->GetWindow();

		sf::CircleShape entityG = sf::CircleShape(15);
		entityG.setFillColor(sf::Color::Yellow);
		entityG.setPosition(m_pLevelManager->GetPlayAreaX(posX), m_pLevelManager->GetPlayAreaY(posY));
		entityG.setOrigin(15, 15);
		pWindow->draw(entityG);


		
		// tick it so it can do whatever it wants.
		pEntity->Tick(DeltaTime);


		// Update its motions.
		float progress = pEntity->GetProgress();
		float speed = pEntity->GetSpeed() * DeltaTime;

		progress = progress + speed;

		if (progress < 1)
		{
			pEntity->SetProgress(progress);
		}
		else
		{
			pEntity->OnNodeReached();
		}
	}
}

//-------------------------------------------------------------------------------------------
void EntityManager::OnInput(sf::Event::KeyEvent* pKeyEvent, bool keyDown)
{
	for (int i = 0; i < m_entityMap.size(); i++)
	{
		m_entityMap[i]->OnInput(pKeyEvent, keyDown);
	}
}

//-------------------------------------------------------------------------------------------
void EntityManager::AddPlayer(int startingNodeID)
{
	GameEntity* pPlayer = (GameEntity*)new PlayerEntity(1);
	pPlayer->SetPosition(startingNodeID);
	m_entityMap.push_back(pPlayer);
}

//-------------------------------------------------------------------------------------------
void EntityManager::AddEntity(int startingNodeID)
{
	GameEntity* pPlayer = (GameEntity*)new SimpleAIEntity(2);
	pPlayer->SetPosition(startingNodeID);
	m_entityMap.push_back(pPlayer);
}

//-------------------------------------------------------------------------------------------